package com.terry.gamehall.gms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.terry.common.utils.PageUtils;
import com.terry.common.utils.R;
import com.terry.gamehall.gms.entity.CategoryEntity;
import com.terry.gamehall.gms.entity.GameEntity;
import com.terry.gamehall.gms.service.CategoryService;
import com.terry.gamehall.gms.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * nes游戏表
 *
 * @author terry
 * @email 1350579085@qq.com
 * @date 2021-08-09 11:23:28
 */
@RestController
@RequestMapping("gms/game")
public class GameController {
    @Autowired
    private GameService gameService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = gameService.queryPage(params);
        List<GameEntity> list = (List<GameEntity>)page.getList();
        list.stream().forEach((gameEntity)->{
            CategoryEntity categoryEntity = categoryService.getById(gameEntity.getCatId());
            if(categoryEntity!=null){
                gameEntity.setCatName(categoryEntity.getName());
            }
        });
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listall")
    public R list(){
        GameEntity gameEntity = new GameEntity() ;
        gameEntity.setShowStatus(1);
        QueryWrapper<GameEntity> gameEntityQueryWrapper = new QueryWrapper<>(gameEntity);
        gameEntityQueryWrapper.lambda().orderByDesc(GameEntity::getSort);
        List<GameEntity> list = gameService.list(gameEntityQueryWrapper);
        return R.ok().put("list", list);
    }
    //更新热度
    @RequestMapping("/upsort")
    public R upsort(Long gameId){
        GameEntity gameEntity = this.gameService.getById(gameId);
        GameEntity gameEntity1 = new GameEntity();
        gameEntity1.setGameId(gameId);
        gameEntity1.setSort((gameEntity.getSort()!=null?gameEntity.getSort():0)+1);
        this.update(gameEntity1);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{gameId}")
    public R info(@PathVariable("gameId") Long gameId){
		GameEntity game = gameService.getById(gameId);
        CategoryEntity categoryEntity = categoryService.getById(game.getCatId());
        if(categoryEntity!=null){
            game.setCatName(categoryEntity.getName());
        }
        return R.ok().put("game", game);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated({}) @RequestBody GameEntity game, BindingResult result){
        if(result.hasErrors()){
            Map<String,String> map = new HashMap<>();
            //获取校验的错误结果
            result.getFieldErrors().forEach((item)->{
            //FieldError 获取到错误提示
            String message =  item.getDefaultMessage();
            //获取到错误的属性名字
            String field = item.getField();
            map.put(field,message);
            });
            return R.error(400,"提交的数据不合法!").put("data",map);
        }else{

        }

        gameService.save(game);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GameEntity game){
		gameService.updateById(game);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] gameIds){
		gameService.removeByIds(Arrays.asList(gameIds));

        return R.ok();
    }

}
